﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Check_Printing_Solution
{
    public partial class ManageCustomers : Form
    {
        PrintManager manager = new PrintManager();
        Operations operations;

        public ManageCustomers()
        {
            InitializeComponent();
        }

        public ManageCustomers( Operations ops )
        {
            operations = ops;
            InitializeComponent();
        }

        private void ManageCustomers_Load( object sender, EventArgs e )
        {
            RefreshDataGrid();
        }

        private void RefreshDataGrid()
        {
            List<Bank> banks = manager.GetAllBanks();
            cmbBanks.DataSource = banks;
            cmbBanks.DisplayMember = "Name";
            cmbBanks.ValueMember = "Id";

            cmbBanksU.DataSource = banks;
            cmbBanksU.DisplayMember = "Name";
            cmbBanksU.ValueMember = "Id";

            dgCustomers.DataSource = manager.GetAllCustomers();
            dgCustomers.Columns["Id"].Visible = false;
            dgCustomers.Columns["Bank"].Visible = false;
            dgCustomers.Columns["BankId"].Visible = false;
            dgCustomers.Columns["Enabled"].Visible = false;
        }

        private void PopulateForm( Customer customer )
        {
            txtFirstName.Text = customer.FirstName;
            txtLastName.Text = customer.LastName;
            txtLine2.Text = customer.Line2;
            txtLine3.Text = customer.Line3;
            txtLine4.Text = customer.Line4;
            txtCheckNo.Text = customer.ChequeNumber;
            txtLine1.Text = customer.Line1;
        }

        private void btnCancel_Click( object sender, EventArgs e )
        {
            this.Close();
        }

        private void btnSave_Click( object sender, EventArgs e )
        {
            if ( txtCheckNo.Text.Trim() != string.Empty )
            {
                Customer customer = new Customer();
                customer.Id = Guid.NewGuid().ToString();
                customer.Line2 = txtLine2.Text;
                customer.Line3 = txtLine3.Text;
                customer.Line4 = txtLine4.Text;
                customer.BankId = cmbBanks.SelectedValue.ToString();
                customer.ChequeNumber = txtCheckNo.Text;
                customer.FirstName = txtFirstName.Text;
                customer.LastName = txtLastName.Text;
                customer.Line1 = txtLine1.Text;

                if ( manager.DoesCustomerExist( customer.ChequeNumber ) )
                {
                    MessageBox.Show( "Customer already exists. " + customer.ChequeNumber, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
                }
                else
                {
                    manager.AddCustomer( customer );
                    ClearCustomerFields();
                    RefreshDataGrid();
                    MessageBox.Show( "Customer " + customer.ChequeNumber + " added successfully", "Customer Added", MessageBoxButtons.OK, MessageBoxIcon.Information );
                }
            }
            else
            {
                MessageBox.Show( "Please add an Account Number for the customer before saving.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
            }
        }

        private void dgCustomers_SelectionChanged( object sender, EventArgs e )
        {
            if ( dgCustomers.SelectedRows.Count > 0 )
            {
                string customerid = dgCustomers.SelectedRows[0].Cells["Id"].Value.ToString();
                PopulateCustomer( manager.GetCustomer( customerid ) );
            }
        }

        private void PopulateCustomer( Customer customer )
        {
            if (customer != null)
            {
                txtFirstNameU.Text = customer.FirstName;
                txtLastNameU.Text = customer.LastName;
                txtLine1U.Text = customer.Line1;
                txtLine2U.Text = customer.Line2;
                txtLine3U.Text = customer.Line3;
                txtLine4U.Text = customer.Line4;
                txtCheckNoU.Text = customer.ChequeNumber;
                cmbBanksU.SelectedValue = customer.BankId;
            }
        }

        private void ClearCustomerFields()
        {
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtLine1.Text = "";
            txtLine2.Text = "";
            txtLine3.Text = "";
            txtCheckNo.Text = "";
        }

        private void btnUpdate_Click( object sender, EventArgs e )
        {
            if ( txtCheckNoU.Text.Trim() != string.Empty )
            {
                Customer customer = new Customer();

                customer.Id = dgCustomers.SelectedRows[0].Cells["Id"].Value.ToString();
                customer.FirstName = txtFirstNameU.Text;
                customer.LastName = txtLastNameU.Text;
                customer.Line1 = txtLine1U.Text;
                customer.Line2 = txtLine2U.Text;
                customer.Line3 = txtLine3U.Text;
                customer.Line4 = txtLine4U.Text;
                customer.ChequeNumber = txtCheckNoU.Text;
                customer.BankId = cmbBanksU.SelectedValue.ToString();

                manager.UpdateCustomer( customer );
                RefreshDataGrid();
                MessageBox.Show( "Customer " + customer.ChequeNumber + " updated successfully", "Customer Updated", MessageBoxButtons.OK, MessageBoxIcon.Information );
            }
            else
            {
                MessageBox.Show( "Please add an Account Number for the customer before saving.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
            }
        }

        private void btnDelete_Click( object sender, EventArgs e )
        {
            DialogResult result = MessageBox.Show( "Are you sure you want to remove this customer?", "Delete Customer", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
            if ( result == System.Windows.Forms.DialogResult.Yes )
            {
                manager.DeleteCustomer( dgCustomers.SelectedRows[ 0 ].Cells[ "Id" ].Value.ToString() );
                RefreshDataGrid();
            }
            MessageBox.Show( "Customer " + dgCustomers.SelectedRows[ 0 ].Cells[ "ChequeNumber" ].Value.ToString() + " deleted successfully", "Customer Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information );
        }

        private void textBox1_TextChanged( object sender, EventArgs e )
        {
            dgCustomers.DataSource = manager.FilterCustomers( textBox1.Text );
            dgCustomers.Columns["Id"].Visible = false;
            dgCustomers.Columns["Bank"].Visible = false;
            dgCustomers.Columns["BankId"].Visible = false;
            dgCustomers.Columns["Enabled"].Visible = false;
        }
    }
}
